#include "../../ExEngine/math/matrix.h"

#include "../../ExEngine/physics/actor.h"
#include "../../ExEngine/physics/physics.h"
#include "../../ExEngine/math/quaternion.h"
#include "../../ExEngine/common/util.h"
#include "../../ExEngine/engine/engine.h"
#include "../../ExEngine/engine/camera.h"
#include "../../ExEngine/render/d3d_mesh.h"
#include "../../ExEngine/render/d3d.h"
#include "../../ExEngine/sound/dsound.h"
#include "../../ExEngine/input/dinput.h"

#include "ent_deathtrigger.h"
#include "player.h"
#include "../game/core.h"

/**********************************************************************
 *
 **********************************************************************/

CDeathTrigger::CDeathTrigger() : CObject()
{
	m_isValid = true;
}

HRESULT CDeathTrigger::Create()
{	
	if(FAILED(CEngine::GetInstance()->GetPhysics()->CreateFieldActor("death_trigger.HKX", &m_pPhysics, m_transform, this)))
	{
		Release();
		return E_FAIL;
	}

	m_pPhysics->SetCollisionCallback(Game::g_Player);

	return S_OK;
}

HRESULT CDeathTrigger::Release()
{	
	HRESULT result = S_OK;
	if(m_pModel)
	{
		m_pModel = NULL;
	}
	if(m_pPhysics)
	{
		CPhysics* pPhysics = CEngine::GetInstance()->GetPhysics();
		if(FAILED(pPhysics->ReleaseActor(m_pPhysics)))
			result = E_FAIL;
		m_pPhysics = NULL;
	}
	return result;
}

void CDeathTrigger::Idle()
{
}

void CDeathTrigger::setPosition(const Vector3f &position)
{
	m_transform = Matrix4f::identity();
	m_transform.SetTranslation(position); 
	if(m_pPhysics)
		m_pPhysics->SetTransform(m_transform);
	
}

void CDeathTrigger::OnCollisionEvent(const Vector3f& contactPoint, const Vector3f& contactNormal, CObject* pOtherObject) {

	if(pOtherObject == Game::g_Player)
	{
		Game::g_Player->setPosition(Game::g_Checkpoint);
		
	}
}

bool CDeathTrigger::isValid() {
	return m_isValid;
}

void CDeathTrigger::Update(const float dt)
{	
}

void CDeathTrigger::Draw()
{	
	GetRenderer()->DrawSphere(m_transform.Translation());
}